<template>
	<view class="share">
		<u-popup v-model="show" :mask="false" mode="bottom" :height="popHeight" :customStyle="{'boxShadow':'0rpx -6rpx 16rpx 4rpx rgba(236,236,236,0.8)'}">
			<view class="main">
				<view class="main_tip" v-if="posterShow">
					{{ $t('sharepop.sharetip') }}
				</view>
				<view class="main_app">
					<view class="main_app_item" @click="share(5)">
						<view class="main_app_item_icon">
							<image src="@/static/image/invite/whatsapp_icon.png" mode=""></image>
						</view>
						<view class="main_app_item_name">
							WhatsApp
						</view>
					</view>
					<view class="main_app_item" @click="share(2)" v-if="!posterShow">
						<view class="main_app_item_icon">
							<image src="@/static/image/invite/telegram_icon.png" mode=""></image>
						</view>
						<view class="main_app_item_name">
							Telegram
						</view>
					</view>
					<view class="main_app_item" @click="share(3)" v-if="!posterShow">
						<view class="main_app_item_icon">
							<image src="@/static/image/invite/copylink_icon.png" mode=""></image>
						</view>
						<view class="main_app_item_name">
							Copy Link
						</view>
					</view>
				</view>
				<!-- <view class="main_line">

				</view>
				<view class="main_btn" @click="close">
					{{ $t('sharepop.cancel') }}
				</view> -->
			</view>
		</u-popup>
		<view class="poster_pop" v-show="posterShow && imgVal">
			<view class="poster">
				<image :src="whatsappImg" mode="heightFix" @load="imgVal = true" v-if="shareType == 2"></image>
				<image :src="posterImg" mode="heightFix" @load="imgVal = true" v-else></image>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		copyString,
		GetUrlParam
	} from '@/utils/utils.js'
	export default {
		props: {
			showPopup: {
				type: Boolean,
				default: false
			},
			height: {
				type: [Number, String],
				default: '265'
				// default: '360'
			},
			shareType: {
				type: [String, Number],  // 分享内容 1客服 2商品链接 3瓜分链接 4理财
				default: ''
			},
			orderID: {
				type: [String, Number],
				default: ''
			},
			financeID: {
				type: [String, Number],
				default: ''
			},
		},
		data() {
			return {
				show: false,
				posterShow: false,
				popHeight: null,
				posterImg: '',
				whatsappImg: '',
				whatsappUrl: '',
				financeurl: '',
				imgVal: false,
				SSDKPlatformID: undefined,
				sendMes: {},
			}
		},
		mounted() {
			this.popHeight = this.height
			// this.SSDKPlatformID = SSDKPlatformID
		},
		watch: {
			showPopup() {
				this.show = this.showPopup
			},
			posterShow(nVal) {
				if (nVal) {
					this.popHeight = '360'
				} else {
					this.popHeight = this.height
				}
			},
			show(nVal) {
				if (!nVal) {
					this.posterShow = this.show
				}
			},
		},
		computed: {
			codeUrl() {
				return this.$store.getters.systemInfo.invite_url || ''
			}
		},
		methods: {
			open() {
				this.show = true
				this.getData()
			},
			close() {
				this.show = false
			},
			// 分享文字
			mobShareText(platName) {
				let _this = this
				console.log(platName, 'platName');
				pluginMobShare.generalShare({
					platName: platName,
					params: {
						text: 'text',
						title: "title",
					},
					shareType: SSDKContentType.Text,
				}, result => {
					const msg = JSON.stringify(result);
					_this.showAlertAndLog(msg);
				});
			},
			share(index) {
				switch (+index) {
					case 0:
						// #ifdef APP-PLUS
						// this.mobShareText(SSDKPlatformID.WhatsApp)
						if (this.$store.state.token) {
							// 打开app添加好友
							plus.runtime.openURL(
								'whatsapp://send?phone=+电话&text=HelloHelloHelloHelloHelloHelloHello')
							// uni.shareWithSystem({
							// 	summary: 'share content',
							// 	href: 'http://orich.in',
							// 	success() {

							// 	}
							// })
						} else {
							uni.showToast({
								icon: 'none',
								title: 'Please log in first',
								position: 'center',
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '../../pages/login/login'
								})
							}, 1000)
						}
						// #endif
						break
					case 1:
						// this.mobShareText(SSDKPlatformID.Facebook)
						let shareUrl = ''
						// let shareURLM = ''
						if (this.shareType == 2) {
							shareUrl = `${this.sendMes.orderurl}?id=${this.orderID}`
						} else if (this.shareType == 3) {
							shareUrl = this.codeUrl
						} else if (this.shareType == 4) {
							let textUrl =  encodeURIComponent(this.financeurl)
							shareUrl = this.financeurl
						} else {
							shareUrl = this.sendMes.content
						}

						// #ifdef APP-PLUS
						plus.runtime.openURL(shareUrl)
						// #endif
						// #ifndef APP-PLUS
						window.open(shareUrl)
						// #endif
						break
					case 2:
						// this.posterShow = true
						// #ifdef APP-PLUS
						if (this.$store.state.token) {
							console.log()
							// 打开app添加好友
							if (plus.runtime.isApplicationExist({pname:'org.telegram.messenger',action:'tg://'})) {
								plus.runtime.openURL(
									`tg://share?text=${this.codeUrl}`)
							} else {
								plus.runtime.openURL(
									`https://t.me/share/url?url=${this.codeUrl}`)
							}

							//https://t.me/share/url?url=https%3A%2F%2Fleefire.com%2Fshare%3FInviteCode%3DREWARD_8037074%26appid%3D999%26lang%3Den-PH
							// uni.shareWithSystem({
							// 	summary: 'share content',
							// 	href: 'http://orich.in',
							// 	success() {

							// 	}
							// })
						} else {
							uni.showToast({
								icon: 'none',
								title: 'Please log in first',
								position: 'center',
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '../../pages/login/login'
								})
							}, 1000)
						}
						// #endif
						// #ifndef APP-PLUS
						window.open(`https://t.me/share/url?url=${encodeURIComponent(this.codeUrl)}`)
						// #endif
						break
					case 3:
							copyString(this.codeUrl)
						break
					case 4:
						if (this.shareType == 2) {
							this.saveImg(this.whatsappImg)
						} else {
							this.saveImg(this.posterImg)
						}
						break
					case 5:
						// #ifdef APP-PLUS
						// this.mobShareText(SSDKPlatformID.WhatsApp)
						if (this.$store.state.token) {
							console.log()
							// 打开app添加好友
							if (plus.runtime.isApplicationExist({pname:'com.whatsapp',action:'whatsapp://'})) {
								plus.runtime.openURL(
									`whatsapp://send?text=${this.codeUrl}`)
							} else {
								plus.runtime.openURL(
									`https://wa.me?text=${this.codeUrl}`)
							}
							// uni.shareWithSystem({
							// 	summary: 'share content',
							// 	href: 'http://orich.in',
							// 	success() {

							// 	}
							// })
						} else {
							uni.showToast({
								icon: 'none',
								title: 'Please log in first',
								position: 'center',
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '../../pages/login/login'
								})
							}, 1000)
						}
						// #endif
						// #ifndef APP-PLUS
						window.open(`https://wa.me/?text=${this.codeUrl}`)
						// #endif
						break
				}
			},
			showAlertAndLog(msg) {
				uni.showModal({
					title: '提示',
					content: msg,
					showCancel: false,
					success: function(res) {

					}
				});
				console.log(msg);
			},
			saveImg(url) {
				// #ifndef H5
				uni.showLoading({
					mask: true,
					title: 'downloading...'
				})
				let _t = this;
				uni.downloadFile({
					url: url,
					success: (res) => {
						if (res.statusCode === 200) {
							let file = res.tempFilePath;
							_t.save(file)
						}
					}
				});
				// #endif
			},
			save(url) {
				let _this = this;
				uni.saveImageToPhotosAlbum({
					filePath: url,
					success: () => {
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							title: this.$t('sharepop.save'),
							position: 'center',
						})
					},
					fail: () => {
						uni.hideLoading();
						uni.showToast({
							icon: 'none',
							title: 'fail',
							position: 'center',
						})
					},
					complete: () => {

					}
				});
			},
			// 分享文字
			mobShareText(platName) { //分享文字
				var _this = this
				pluginMobShare.generalShare({
					platName: platName,
					params: {
						text: 'text',
						title: "title",
					},
					shareType: SSDKContentType.Text,
				}, result => {
					const msg = JSON.stringify(result);
					_this.showAlertAndLog(msg);
				});
			
			},
			async getData () {
				// await systemService({ type: 1 }).then(res => {
				// 	this.sendMes = res
				// })
			}
			
		},
	}
</script>

<style lang="scss" scoped>
	/deep/ .u-drawer {
		z-index: 9999999 !important;
	}
	.main {
		background-color: #fff;
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		// justify-content: space-between;
		
		padding-top: 60rpx;

		.main_tip {
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC-Medium;
			font-weight: 500;
			text-align: CENTER;
			color: #17273a;
			margin-bottom: 30rpx;
		}

		.main_app {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-around;
			padding: 0 30rpx;

			.main_app_item {
				// &:not(:last-child) {
				// 	margin-right: 110rpx;
				// }

				.main_app_item_icon {
					width: 90rpx;
					height: 90rpx;
					margin: 0 auto;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.main_app_item_name {
					margin-top: 15rpx;
					font-weight: 400;
					color: #AAAAAA;
					font-size: 26rpx;
				}
			}
		}

		.main_line {
			width: 100%;
			height: 20rpx;
			background-color: #f5f5f5;
			margin-top: 24rpx;
		}

		.main_btn {
			width: 100%;
			font-size: 32rpx;
			font-weight: 500;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-family: PingFang SC, PingFang SC-Medium;
			color: $uni-text-color-primary;
		}
	}

	.poster_pop {
		position: fixed;
		left: 50%;
		bottom: 400rpx;
		transform: translateX(-50%);
		z-index: 999999999 !important;
		border-radius: 30rpx;
		overflow: hidden;

		.poster {
			// width: 480rpx;
			height: 836rpx;
			background: #fff;

			image {
				// width: 100%;
				height: 100%;
			}
		}
	}
</style>
